package me.firstapp.repository.weixin.system.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import me.firstapp.module.weixin.system.Operator;
import me.firstapp.repository.base.impl.EntityRepositoryImpl;
import me.firstapp.repository.weixin.system.OperatorRepository;

@Repository
public class OperatorRepositoryImpl extends EntityRepositoryImpl<Operator> implements OperatorRepository {

	@Override
	protected Class<Operator> getPersistentClass() {
		return Operator.class;
	}

	@Override
	public Operator findOperatorByName(String name) {
		StringBuilder sql = new StringBuilder(" FROM Operator bean WHERE 1=1 ");
		Map<String, Object> parameters = new HashMap<>();

		sql.append(" AND bean.name = :name ");
		parameters.put("name", name);

		List<Operator> result = super.query(Operator.class, sql.toString(), parameters);
		return result.isEmpty() ? null : result.get(0);
	}

	@Override
	public Operator findOperatorByEmail(String email) {
		StringBuilder sql = new StringBuilder(" FROM Operator bean WHERE 1=1 ");
		Map<String, Object> parameters = new HashMap<>();

		sql.append(" AND bean.email = :email ");
		parameters.put("email", email);

		List<Operator> result = super.query(Operator.class, sql.toString(), parameters);
		return result.isEmpty() ? null : result.get(0);
	}

}
